package com.shop.shopcommon.token;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// 自定义用户类，继承自 Spring Security 的 User 类
public class CustomUser extends User {

    // 额外的用户属性，用户的ID
    private Long id;

    // 构造方法，调用父类的构造方法初始化基本用户信息，并初始化额外属性
    public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities, Long id) {
        super(username, password, authorities);
        this.id = id;
    }

    // 构造方法，调用父类的构造方法初始化基本用户信息，并初始化额外属性，同时指定账户是否可用等状态
    public CustomUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities, Long id) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.id = id;
    }

    // 获取额外属性的方法
    public Long getId() {
        return id;
    }

    // 设置额外属性的方法
    public void setId(Long id) {
        this.id = id;
    }

    // 静态内部类，用于实现构造者模式
    public static class Builder {
        private String username;
        private String password;
        private boolean enabled = true;
        private boolean accountNonExpired = true;
        private boolean credentialsNonExpired = true;
        private boolean accountNonLocked = true;
        private List<GrantedAuthority> authorities;
        private Long id;

        public Builder withUsername(String username) {
            this.username = username;
            return this;
        }

        public Builder withPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder disabled(boolean disabled) {
            this.enabled = !disabled;
            return this;
        }

        public Builder authorities(Collection<? extends GrantedAuthority> authorities) {
            this.authorities = new ArrayList(authorities);
            return this;
        }

        public Builder authorities(String... authorities) {
            return this.authorities((Collection) AuthorityUtils.createAuthorityList(authorities));
        }

        public Builder withId(Long id) {
            this.id = id;
            return this;
        }

        public CustomUser build() {
            return new CustomUser(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id);
        }
    }

    // 静态方法，用于创建 Builder 实例，重命名为 customBuilder()
    public static Builder customBuilder() {
        return new Builder();
    }
}